<template>
  <div class='container' >
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <!-- 搜索行 -->
      <el-row type="flex" class="sarch" >
        <el-col :span="1" class="txt" >关键字</el-col>
        <!-- 搜索框 -->
        <el-col :span="5" ><el-input v-model="query.keyword" placeholder="根据编号搜索"></el-input></el-col>
        <el-col :span="1" :offset="16"  ><el-button size="small" @click="query.keyword = ''">清除</el-button></el-col>
        <el-col :span="1" ><el-button size="small" @click="onSearch" type="primary">搜索</el-button></el-col>
      </el-row>
      <!-- 消息提示 -->
      <el-alert
        :title="'数据一共' +total+ '条'  "
        type="info"
        show-icon>
      </el-alert>
      <!-- 表格部分 -->
      <el-table
        :data="randomsList"
        style="width: 100%">
        <el-table-column
          prop="id"
          label="编号"
          width="220">
        </el-table-column>
        <el-table-column
          prop="questionType"
          label="题型"
          width="80">
          <template slot-scope="{row}">
            {{row.questionType | changeNumber}}
          </template>
        </el-table-column>
        <el-table-column
          prop="questionIDs"
          label="题目编号"
          width="220"
          >
          <template slot-scope="{row}">
            <div class="questionNumber" @click="onShowQuestion(item.id)" v-for="item in randomsNumber(row.questionIDs)" :key="item.number">{{item.number}}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="addTime"
          label="录入时间"
          width="180"
          >
        </el-table-column>
        <el-table-column
          prop="totalSeconds"
          label="答题时间"
          width="295"
          >
        </el-table-column>
        <el-table-column
          prop="accuracyRate"
          label="正确率(%)"
          width="293"
          >
        </el-table-column>
        <el-table-column
          prop="userName"
          label="录入人"
          width="293"
          >
        </el-table-column>
        <el-table-column
          prop="address"
          label="操作"
          width="80"
          >
          <template slot-scope="{row}" >
             <el-button @mouseenter.native="entrer(row)" @mouseleave.native="isActive = true" :class="{deleteBtn: isActive !== row.id}" @click="onDelete(row.id)" size="mini" type="danger" icon="el-icon-delete" circle></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <div class="paging">
        <el-row >
          <el-col :offset="13">
            <el-pagination
            background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="query.page"
          :page-sizes="[20, 30, 50, 100]"
          :page-size="query.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
          </el-col>
        </el-row>
      </div>
    </el-card>
    <!-- /卡片区域 -->
    <!-- 试题预览对话框 -->
    <questions-preview :showQuestion="showQuestion" :randomID="randomID" @close="showQuestion = false" />
  </div>
</template>

<script>
import { randoms as randomsList, removeRandoms } from '@/api/hmmm/questions.js'
import questionsPreview from '../components/questions-preview.vue'
export default {
  components: { questionsPreview },
  name: 'randoms',
  data () {
    return {
      showQuestion: false,
      randomsList: [],
      input: '',
      total: 0,
      query: {
        page: 1,
        pagesize: 20,
        keyword: ''
      },
      randomID: 0,
      isActive: true
    }
  },

  computed: {

  },

  created () {
    this.getRandomsList(this.query)
  },

  filters: {
    changeNumber (val) {
      if (val === '0') {
        return '单选'
      } else if (val === '1') {
        return '多选'
      } else {
        return '简答'
      }
    }
  },

  methods: {
    // 获取主题列表
    async getRandomsList (data) {
      const res = await randomsList(data)
      this.randomsList = res.data.items
      this.total = res.data.counts
      // console.log(res)
    },
    // 编译输出编号
    randomsNumber (data) {
      // console.log(data)
      return data.map(item => item)
    },
    // 删除数据
    onDelete (id) {
      this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 点击确定之后的操作
        await removeRandoms({ id: id })
        this.getRandomsList(this.query)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        console.log('取消删除')
      })
    },
    // 页面尺寸变化
    async handleSizeChange (val) {
      this.query.pagesize = val
      this.getRandomsList(this.query)
    },
    // 当前页面发生变化
    handleCurrentChange (val) {
      this.query.page = val
      this.getRandomsList(this.query)
    },
    // 搜索按钮
    onSearch () {
      this.getRandomsList(this.query)
    },
    // 展示试题预览的对话框
    onShowQuestion (val) {
      this.showQuestion = true
      this.randomID = val
      // console.log(val)
    },
    // 鼠标经过的样式
    entrer (val) {
      this.isActive = val.id
    }
  }

}
</script>

<style scoped lang='less'>
.container{
  padding: 10px;
  .el-row {
    align-items: center;
    margin-bottom: 20px;
    .txt{
      font-size: 14px;
      color: #606266;
      font-weight: 700;
      width: 50px;
    }
  }
  .paging{
    margin-top: 20px;

  }
  .deleteBtn{
    background-color: #fef0f0;
    color :#f56c6c
  }
  .sarch {
    align-items: center;
  }
  .questionNumber{
    color: rgb(0, 153, 255);
    cursor: pointer;
  }
}
</style>
